<?php
        session_start();
	if(isset($_SESSION['login_username']) && $_SESSION['login_username']==$_GET['username']){
            require_once("functions.php");
            
            $formvalid = false;
            $errors['email'] = 0;
            $errors['avatar'] = 0;
            $errors['blank_email'] = false;
            $formvalid = false;
            if(isset($_POST['submit'])){
                    if($_POST['email']==$_SESSION['login_email'])
                        $errors['email'] = $_POST['email']==$_SESSION['login_password'];
                    $errors['avatar'] = validateAvatar($_FILES['avatar']['type']);
                    $errors['blank_email'] = $_POST['email']=='';
                    $formvalid = !$errors['blank_email'] && !$errors['email'] && !$errors['avatar'];
                    if($formvalid){
                        require_once('db.php');
                        //This is the directory where images will be saved 
                        $target = "user_avatar/"; 
                        $target = $target . basename( $_FILES['avatar']['name']);
                        $pic=($_FILES['avatar']['name']);
                        move_uploaded_file($_FILES['avatar']['tmp_name'], $target);
                        if(connect_db()){                
                                $sql = "UPDATE pengguna SET email = '".$_POST['email']."', avatar = '".$pic."', aboutme = '".$_POST['aboutme']."' WHERE username = '".$_SESSION['login_username']."';";
                                if (!mysql_query($sql))
                                        printf('A database error occurred in processing your '.
                                        'submission.<br />If this error persists, please '.
                                        'contact admin@pobunesia.org');
                                else{
                                        $_SESSION['login_email'] = $_POST['email'];
                                        $_SESSION['login_avatar'] = $pic;
                                        $_SESSION['login_aboutme'] = $_POST['aboutme'];
                                }
                        }
                    }
            }
  
	require_once("header.php");       
?>
        <div id="content">                    
                <div id="content-title">
                        <?php echo $_GET['username']."'s Profile"; ?>
                </div>
                <div id="profile">
                        <div id="profile_avatar">
                                <img src=<?php echo "user_avatar/".$_SESSION['login_avatar']; ?> width="300px" height="300px" alt="avatar">
                        </div>
                        <div id="profile_info">
                                <table>                                            
                                        <tr>
                                                <td>Username</td>
                                                <td><?php echo $_SESSION['login_username'];?></td>
                                        </tr>                                      
                                        <tr>
                                                <td>Email</td>
                                                <td><?php echo $_SESSION['login_email'];?></td>
                                        </tr>
                                        <tr>
                                                <td>Gender</td>
                                                <td><?php echo $_SESSION['login_sex'];?></td>
                                        </tr>
                                        <tr>
                                                <td>About me</td>
                                                <td><?php echo $_SESSION['login_aboutme'];?></td>
                                        </tr>
                                        <tr>
                                                <td>achievements</td>
                                                <td><?php echo "username";?></td>
                                        </tr>
                                        <tr>
                                                <td>jumlah komentar</td>
                                                <td><?php echo "username";?></td>
                                        </tr>
                                        <tr>
                                                <td>jumlah upload</td>
                                                <td><?php echo "username";?></td>
                                        </tr>
                                </table>
                        </div>
                </div>
                <div id="content-title">
                        <?php echo "Edit ".$_GET['username']."'s Profile"; ?>
                </div>
                <div id="content-body">
                <?php if($formvalid) { echo "Profil update success!";}?>
                    <form id="reg_form" action="" method="post" enctype="multipart/form-data">
                            <ul>
                                    <li>
                                            <div class="prompt">Email</div><input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; else echo $_SESSION['login_email'];?>" onkeyup="checkUsernameEmail();">
                                            <?php if($errors['blank_email']){ ?>
                                            <div class="error_msg">Do not leave email blank!</div>
                                            <?php } else {
                                                    switch($errors['email']){
                                                            case 1 : ?>
                                                            <div class="error_msg" id="error_email">
                                                                    Differ from password<br />
                                                            </div>						
                                                            <?php break;
                                                            case 2 : ?>
                                                            <div class="error_msg" id="error_email">
                                                                    Min 1 char before @<br />
                                                                    Min 1 char between @ and .<br />
                                                                    Min 2 char after .<br />
                                                            </div>						
                                                            <?php break;
                                                            case 3 : ?>
                                                            <div class="error_msg" id="error_email">
                                                                    E-mail has been registered<br />
                                                            </div>
                                                            <?php break;
                                                    }
                                            }
                                            ?>
                                            <div class="error_msg" id="email_respond"></div>
                                    </li>
                                    <li>
                                            <div class="prompt">Avatar</div><input type="file" name="avatar" accept="image/jpeg">
                                            <?php
                                                    switch($errors['avatar']){
                                                            case 1 : ?>
                                                            <div class="error_msg" id="error_avatar">Choose jpg/jpeg one!</div>
                                                            <?php break;
                                                    }
                                            ?>				
                                    </li>
                                    <li>
                                            <div class="prompt">About Me</div><textarea rows="5" cols="40" name="aboutme" placeholder="Saya adalah seorang yang ..."><?php echo $_SESSION['login_aboutme']; ?></textarea>
                                    </li>
                                    <li>
                                            <div class="prompt"><input type="submit" value="Submit" name="submit"></div>
                                    </li>
                            </ul>
                    </form>
                    <?php
                    require_once('db.php');
                    if(connect_db()){                
                $sql = "SELECT * FROM pengguna WHERE username='".$_SESSION['login_username']."'";
                $result = mysql_query($sql);
                if (!$result){
                    printf('A database error occurred in processing your '.
                    'submission.<br />If this error persists, please '.
                    'contact admin@pobunesia.org');
                }else{                    
                    $count=mysql_num_rows($result);
                    $known=false;
                    if($count>0){
                        $known=true;
                        $row = mysql_fetch_array($result);
                        $aboutme = $row['aboutme'];
                        $sex = $row['gender']; 
                        ?>

                    <div id="posts">
                    <div id="postprofile-title">
                            <br />
                            <?php echo $_SESSION['login_username']; ?>'s Posts
                    </div>
           <div id="isi-content">
				<?php
					$con = mysql_connect("localhost","progin","progin");
				
					if (!$con) {
						die('Could not connect: ' . mysql_error());
					}

					mysql_select_db("progin_171_13509045", $con) or die("Cannot find database");
					
					$tbl_name="konten";
					$adjacents = 3;
                                        if(isset($_GET['tag']))
                                            $tag = $_GET['tag'];
                                        else
                                            $tag = 'nofilter';
					
					$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE username='".$_GET['username']."'";
                                        //echo $query;
					
					$total_content = mysql_fetch_array(mysql_query($query));
					$total_content = $total_content['num'];
	
					$targetpage = "user.php";
					$limit = 10;
                                        if(isset($_GET['page']))
                                            $page = $_GET['page'];
                                        else
                                            $page = 1;
					
					if ($page) {
						$start = ($page - 1) * $limit;
					}
					else {
						$start = 0;
					}
	
					
                                        $sql = "INSERT INTO tagfilter SELECT * FROM $tbl_name WHERE username='".$_GET['username']."'";
					$result = mysql_query($sql);
					$sql = "SELECT * FROM tagfilter LIMIT $start, $limit";
					$result = mysql_query($sql);
					
					
					if ($page == 0) $page = 1;
					$prev = $page - 1;
					$next = $page + 1;
					$lastpage = ceil($total_content/$limit);
					$lpm1 = $lastpage - 1;
	
					$pagination = "";
					if ($lastpage > 1) {	
						$pagination .= "<div class=\"pagination\">";
						//previous button
						if ($page > 1) {
							$pagination.= "<a href=\"$targetpage?page=$prev&tag=$tag\">previous </a>";
						}
						else {
							$pagination.= "<span class=\"disabled\">previous </span>";
						}
		
						//pages	
						if ($lastpage < 7 + ($adjacents * 2)) {	
							for ($counter = 1; $counter <= $lastpage; $counter++) {
								if ($counter == $page) {
									$pagination.= "<span class=\"current\">$counter </span>";
								}
								else {
									$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";
								}
							}
						}
						else if ($lastpage > 5 + ($adjacents * 2))	{ //enough pages to hide some
							//close to beginning; only hide later pages
							if($page < 1 + ($adjacents * 2)) {
								for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter </span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";		
									}
								}
								
								$pagination.= "...";
								$pagination.= "<a href=\"$targetpage?page=$lpm1&tag=$tag\">$lpm1 </a>";
								$pagination.= "<a href=\"$targetpage?page=$lastpage&tag=$tag\">$lastpage </a>";		
							}
							//in middle; hide some front and some back
							else if ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
								$pagination.= "<a href=\"$targetpage?page=1&tag=$tag\">1 </a>";
								$pagination.= "<a href=\"$targetpage?page=2&tag=$tag\">2 </a>";
								$pagination.= "...";
							
								for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter</span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";		
									}
								}
							
								$pagination.= "...";
								$pagination.= "<a href=\"$targetpage?page=$lpm1&tag=$tag\">$lpm1 </a>";
								$pagination.= "<a href=\"$targetpage?page=$lastpage&tag=$tag\">$lastpage </a>";		
							}
							//close to end; only hide early pages
							else {
								$pagination.= "<a href=\"$targetpage?page=1&tag=$tag\">1 </a>";
								$pagination.= "<a href=\"$targetpage?page=2&tag=$tag\">2 </a>";
								$pagination.= "...";
				
								for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter </span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";
									}
								}
							}
						}
		
						//next button
						if ($page < $counter - 1) {
							$pagination.= "<a href=\"$targetpage?page=$next&tag=$tag\">next</a>";
						}
						else {
							$pagination.= "<span class=\"disabled\">next</span>";
							$pagination.= "</div>\n";
						}
					}

					print "<table>";		
					$increment = 1;
					
					while ($row = mysql_fetch_array($result)) {
						$labelid = "likecount" + $increment;
						$contentID = $row['id_konten'];
						$like = $row['jumlahlike'];
						echo "<h3><a href=\"content.php?contentid=".$row['id_konten']."\" style=\"text-decoration:none;\">";
						echo $row['judul'];
						print '</a></h3>
								<img border="0" src="'.$row['url'].'" alt="Pulpit rock" width="304" height="228" />
								<br/>
								<div id="likedislike">';						
						echo "<input type='button' id='likebutton' value='like' onclick=\"changeLike('$contentID', '$like', '$labelid', 'up');\"></input>
								<input type='button' id='dislikebutton' value='dislike' onclick=\"changeLike('$contentID', '$like', '$labelid', 'down');\"></input>
								<label id='$labelid'>";
						echo $row['jumlahlike'];
						print ' likes</label>
								</div>';
						$increment = $increment + 1;
					}

					print "</table>";
					
					echo "<div>
								<a href=\"$targetpage?page=1&tag=nofilter\">Lihat Semua Tanpa Tag</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Bencana akibat korupsi\">Bencana akibat korupsi</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Kejahatan terhadap alam\">Kejahatan terhadap alam</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Pelanggaran lalu lintas\">Pelanggaran lalu lintas</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Korupsi\">Korupsi</a>
							</div>";
							
					$sql ="TRUNCATE TABLE tagfilter";
					$result = mysql_query($sql);
	
					mysql_close($con);
				?> 
			</div>	
                    </div>		

<?php
                    }else{
                        echo "unknown user";
                    }
                }
            }
        ?>
                </div>
        </div>
	<?php } else { 
            require_once('db.php');
            if(connect_db()){                
                $sql = "SELECT * FROM pengguna WHERE username='".$_GET['username']."'";
                $result = mysql_query($sql);
                if (!$result){
                    printf('A database error occurred in processing your '.
                    'submission.<br />If this error persists, please '.
                    'contact admin@pobunesia.org');
                }else{                    
                    $count=mysql_num_rows($result);
                    $known=false;
                    if($count>0){
                        $known=true;
                        $row = mysql_fetch_array($result);
                        $aboutme = $row['aboutme'];
                        $sex = $row['gender'];                       
                        require_once("header.php");
                        ?>

            <div id="content">
                    <div id="content-title">
                            <?php echo $_GET['username']; ?>
                    </div>

                    <div id="profile">
                            <div id="profile_avatar">
                                    <img src="img/Orang-miskin.jpg" width="300px" height="300px" alt="avatar">
                            </div>
                            <div id="profile_info">
                                    <table>
                                            <tr>
                                                    <td>username</td>
                                                    <td><?php echo $_GET['username'];?></td>
                                            </tr>
                                            <tr>
                                                    <td>gender</td>
                                                    <td><?php echo $sex;?></td>
                                            </tr>
                                            <tr>
                                                    <td>about me</td>
                                                    <td><?php echo $aboutme;?></td>
                                            </tr>
                                            <tr>
                                                    <td>achievements</td>
                                                    <td><?php echo "username";?></td>
                                            </tr>
                                            <tr>
                                                    <td>jumlah komentar</td>
                                                    <td><?php echo "username";?></td>
                                            </tr>
                                            <tr>
                                                    <td>jumlah upload</td>
                                                    <td><?php echo "username";?></td>
                                            </tr>
                                    </table>
                            </div>
                    </div>
                    <div id="posts">
                    <div id="postprofile-title">
                            <?php echo $_GET['username']; ?>'s Posts
                    </div>
           <div id="isi-content">
				<?php
					$con = mysql_connect("localhost","progin","progin");
				
					if (!$con) {
						die('Could not connect: ' . mysql_error());
					}

					mysql_select_db("progin_171_13509045", $con) or die("Cannot find database");
					
					$tbl_name="konten";
					$adjacents = 3;
                                        if(isset($_GET['tag']))
                                            $tag = $_GET['tag'];
                                        else
                                            $tag = 'nofilter';
					
					$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE username='".$_GET['username']."'";
                                        //echo $query;
					
					$total_content = mysql_fetch_array(mysql_query($query));
					$total_content = $total_content['num'];
	
					$targetpage = "user.php";
					$limit = 10;
                                        if(isset($_GET['page']))
                                            $page = $_GET['page'];
                                        else
                                            $page = 1;
					
					if ($page) {
						$start = ($page - 1) * $limit;
					}
					else {
						$start = 0;
					}
	
					
                                        $sql = "INSERT INTO tagfilter SELECT * FROM $tbl_name WHERE username='".$_GET['username']."'";
					$result = mysql_query($sql);
					$sql = "SELECT * FROM tagfilter LIMIT $start, $limit";
					$result = mysql_query($sql);
					
					
					if ($page == 0) $page = 1;
					$prev = $page - 1;
					$next = $page + 1;
					$lastpage = ceil($total_content/$limit);
					$lpm1 = $lastpage - 1;
	
					$pagination = "";
					if ($lastpage > 1) {	
						$pagination .= "<div class=\"pagination\">";
						//previous button
						if ($page > 1) {
							$pagination.= "<a href=\"$targetpage?page=$prev&tag=$tag\">previous </a>";
						}
						else {
							$pagination.= "<span class=\"disabled\">previous </span>";
						}
		
						//pages	
						if ($lastpage < 7 + ($adjacents * 2)) {	
							for ($counter = 1; $counter <= $lastpage; $counter++) {
								if ($counter == $page) {
									$pagination.= "<span class=\"current\">$counter </span>";
								}
								else {
									$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";
								}
							}
						}
						else if ($lastpage > 5 + ($adjacents * 2))	{ //enough pages to hide some
							//close to beginning; only hide later pages
							if($page < 1 + ($adjacents * 2)) {
								for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter </span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";		
									}
								}
								
								$pagination.= "...";
								$pagination.= "<a href=\"$targetpage?page=$lpm1&tag=$tag\">$lpm1 </a>";
								$pagination.= "<a href=\"$targetpage?page=$lastpage&tag=$tag\">$lastpage </a>";		
							}
							//in middle; hide some front and some back
							else if ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
								$pagination.= "<a href=\"$targetpage?page=1&tag=$tag\">1 </a>";
								$pagination.= "<a href=\"$targetpage?page=2&tag=$tag\">2 </a>";
								$pagination.= "...";
							
								for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter</span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";		
									}
								}
							
								$pagination.= "...";
								$pagination.= "<a href=\"$targetpage?page=$lpm1&tag=$tag\">$lpm1 </a>";
								$pagination.= "<a href=\"$targetpage?page=$lastpage&tag=$tag\">$lastpage </a>";		
							}
							//close to end; only hide early pages
							else {
								$pagination.= "<a href=\"$targetpage?page=1&tag=$tag\">1 </a>";
								$pagination.= "<a href=\"$targetpage?page=2&tag=$tag\">2 </a>";
								$pagination.= "...";
				
								for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
									if ($counter == $page) {
										$pagination.= "<span class=\"current\">$counter </span>";
									}
									else {
										$pagination.= "<a href=\"$targetpage?page=$counter&tag=$tag\">$counter </a>";
									}
								}
							}
						}
		
						//next button
						if ($page < $counter - 1) {
							$pagination.= "<a href=\"$targetpage?page=$next&tag=$tag\">next</a>";
						}
						else {
							$pagination.= "<span class=\"disabled\">next</span>";
							$pagination.= "</div>\n";
						}
					}

					print "<table>";		
					$increment = 1;
					
					while ($row = mysql_fetch_array($result)) {
						$labelid = "likecount" + $increment;
						$contentID = $row['id_konten'];
						$like = $row['jumlahlike'];
						echo "<h3><a href=\"content.php?contentid=".$row['id_konten']."\" style=\"text-decoration:none;\">";
						echo $row['judul'];
						print '</a></h3>
								<img border="0" src="'.$row['url'].'" alt="Pulpit rock" width="304" height="228" />
								<br/>
								<div id="likedislike">';						
						echo "<input type='button' id='likebutton' value='like' onclick=\"changeLike('$contentID', '$like', '$labelid', 'up');\"></input>
								<input type='button' id='dislikebutton' value='dislike' onclick=\"changeLike('$contentID', '$like', '$labelid', 'down');\"></input>
								<label id='$labelid'>";
						echo $row['jumlahlike'];
						print ' likes</label>
								</div>';
						$increment = $increment + 1;
					}

					print "</table>";
					
					echo "<div>
								<a href=\"$targetpage?page=1&tag=nofilter\">Lihat Semua Tanpa Tag</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Bencana akibat korupsi\">Bencana akibat korupsi</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Kejahatan terhadap alam\">Kejahatan terhadap alam</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Pelanggaran lalu lintas\">Pelanggaran lalu lintas</a>
							</div>
							<div>
								<a href=\"$targetpage?page=1&tag=Korupsi\">Korupsi</a>
							</div>";
							
					$sql ="TRUNCATE TABLE tagfilter";
					$result = mysql_query($sql);
	
					mysql_close($con);
				?> 
			</div>	
                    </div>		
            </div>
<?php
                    }else{
                        echo "unknown user";
                    }
                }
            }
        ?>
    <?php } ?>
<?php require_once("footer.php"); ?>
